import com.kristou.urgLibJ.Connection.EthernetConnection;
import com.kristou.urgLibJ.RangeSensor.Capture.CaptureData;
import com.kristou.urgLibJ.RangeSensor.Capture.CaptureSettings;
import com.kristou.urgLibJ.RangeSensor.RangeSensorInformation;
import com.kristou.urgLibJ.RangeSensor.UrgDevice;
import java.awt.BorderLayout;
import java.awt.Color;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

class LidarWindow extends JFrame
{
    JPanel myPanel1 = new JPanel();//���1
    JPanel myPanel2 =new JPanel();//���2
    JButton button1 = new JButton("Connect");//��ť1
    JTextArea textarea = new JTextArea("",50,60);
    JScrollPane scrollPane = new JScrollPane(textarea);
    
    JSplitPane jSplitPane =new JSplitPane();//�趨Ϊ���Ҳ�ֲ���
    public LidarWindow()
    {
        init();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    void init()
    {
        jSplitPane.setOneTouchExpandable(true);//�÷ָ�����ʾ����ͷ
        jSplitPane.setContinuousLayout(true);//������ͷ���ػ�ͼ��
        //jSplitPane.setPreferredSize(new Dimension (100,200));
        jSplitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);//���÷ָ��߷���
        myPanel1.setSize(100, 400);
        myPanel2.setSize(500, 400);
        jSplitPane.setLeftComponent(myPanel1);//������������ �����1
        jSplitPane.setRightComponent(myPanel2);//������2
        jSplitPane.setDividerSize(1);//���÷ָ��ߵĿ��
        jSplitPane.setDividerLocation(100);//���÷ָ���λ������
        //jSplitPane.setDividerLocation(400);//�趨�ָ��ߵľ�����ߵ�λ��
        setContentPane(jSplitPane);
        //pack();

        myPanel1.add(button1);
        myPanel2.add(scrollPane);
        //add(myPanel1);
        myPanel1.setBorder(BorderFactory.createLineBorder(Color.green));
        //add(myPanel2);
        myPanel2.setBorder(BorderFactory.createLineBorder(Color.red));
    }

}

public class Lidar  
{
    public static void main(String[] args) 
    {
        
        
    	final LidarWindow window = new LidarWindow();
        window.setTitle("Lidar");
        window.setBounds(100, 100, 600, 400);
                
        UrgDevice device = new UrgDevice(new EthernetConnection());
        int times = 5;
        int failedtime = 0;
        
        boolean work = true;
        int i = 0;
                
        if (device.connect("192.168.0.10")) {
            System.out.println("connected");

            //Get the sensor information
            RangeSensorInformation info = device.getInformation();
            if(info != null){
                System.out.println("Sensor model:" + info.product);
                System.out.println("Sensor serial number:" + info.serial_number);
            }else{
                System.out.println("Sensor error:" + device.what());
            }

            //Set the continuous capture type, Please refer to the SCIP protocol for further details
            device.setCaptureMode(CaptureSettings.CaptureMode.MD_Capture_mode);

            //We set the capture type to a continuous mode so we have to start the capture
            device.startCapture();

            while(work){
                //Data reception happens when calling capture
                CaptureData data = device.capture();
                if(data != null) {
                    window.textarea.append("Scan " + (i + 1) + " , stesps " + data.steps.size() + "\n");
                    window.textarea.update(window.textarea.getGraphics());
                    for(int j = 0; j<data.steps.size();j++){
                        window.textarea.append(i + " distances.size = " + data.steps.get(j).distances.size() + "\n");
                        window.textarea.append(i + " intensities.size = " + data.steps.get(j).intensities.size() + "\n");
                        window.textarea.update(window.textarea.getGraphics());
                    }
                    i++;
                } else {
                    window.textarea.append(" Sensor error:" + device.what() + "\n");
                    window.textarea.update(window.textarea.getGraphics());
                    failedtime ++;
                }
                if(failedtime > times){
                    work = false;
                }
            }

            //Stop the cature
            device.stopCapture();

            //Disconnect from the sensor
            device.disconnect();

        } else {
            window.textarea.append("not connected: " + device.what() + "\n");
        }
        //window.textarea.update(window.textarea.getGraphics());
	
    }
}
